<template>
  <!-- <van-list
    v-model="loading"
    :finished="finished"
    finished-text="已显示全部评论"
    :error="error"
    error-text="加载失败，请点击重试"
    :immediate-check="false"
    @load="onLoad"
  /> -->
  <van-list
    v-model="loading"
    :finished="finished"
    finished-text="没有更多了"
    @load="onLoad"
  >
    <comment-item
      v-for="(item, index) in list"
      :key="index"
      :comment="item"
      @reply-click="$emit('reply-click', $event)"
    />
  </van-list>
</template>

<script>
import { getcommentlist } from '@/api/article.js'
import CommentItem from '../../../components/CommentItem.vue'

export default {
  components: { CommentItem },

  props: {
    type: {
      type: String,
      default: 'a'
    },
    source: {
      type: [Number, String, Object],
      required: true
    },
    list: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      loading: false,
      finished: false,
      offset: null, // 获取下一页数据的标记
      limit: 10,
      error: false
    }
  },

  created() {

  },

  methods: {
    async onLoad() {
      try {
        const res = await getcommentlist({
          type: this.type,
          source: this.source,
          offset: this.offset,
          limit: this.limit
        })
        // console.log(res.last_id)
        // console.log(res.total_count)
        this.list.push(...res.results)

        // 把文章评论的总数量传递到外部
        // this.$emit('onload-success', res.total_count)

        // this.loading = false

        // 4. 判断是否还有数据
        // if (res.results.length) {
        //   // 有就更新获取下一页的数据页码
        //   this.offset = res.last_id
        // } else {
        //   // 没有就将 finished 设置结束
        //   this.finished = true
        // }
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
